﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Inspection
{
    public partial class 原材料复检判定单_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            String sql = @"
Select distinct [原料到发记录].[ID],
                [原料到发记录].[车号],
                原料供货单位.名称,
                dbo.原料.品名, 
                dbo.原料.钢级,
                sum([原料到发明细].重量) as 名义重量合计,
                sum([原料到发明细].支数) as 实际支数合计,
                [原料到发记录].[到货日期],
                [原料到发记录].[质检结果] 
from [原料到发记录] inner join [原料到发明细] on 
     [原料到发记录].ID=[原料到发明细].记录ID inner join 原料供货单位 on 
     原料供货单位.ID=[原料到发记录].供货ID  inner join 原料 on 原料.ID=[原料到发明细].[原料ID]
group by [原料到发记录].[ID],dbo.原料.品名,dbo.原料.钢级,[原料到发记录].[车号],原料供货单位.名称,[原料到发记录].[到货日期],[原料到发记录].[质检结果]";           
            
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql + " order by ID desc";
                this.ViewState["SelectCommand"] = sql + " order by ID desc";
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        //用来供前台调用的一段代码，目的是更新GRIDVIEW
        protected void button_Click(object sender, EventArgs e)
        {
             GridView1.DataBind();
        }
        //通用的分页例程
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }
        //通用的分页例程
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        //通用的分页例程
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }

        //查询
        protected void Button_Query_Click(object sender, EventArgs e)
        {
           // string temp = this.DanHao.Text.Trim().Replace("'", "");
            string Sql = "where 0=0 ";

            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [原料到发记录].[到货日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [原料到发记录].[到货日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and [原料到发记录].[供货ID]=" + this.GongHuoDanWei.Text;
            }
            if (this.YuanCaiLiao.Text != "0")
            {
                Sql += " and [原料到发明细].[原料ID]=" + this.YuanCaiLiao.Text;
            }
            this.SqlDataSource1.SelectCommand = @"
Select distinct [原料到发记录].[ID],
                [原料到发记录].[车号],
                原料供货单位.名称,
                dbo.原料.品名, 
                dbo.原料.钢级,
                sum([原料到发明细].重量) as 名义重量合计,
                sum([原料到发明细].支数) as 实际支数合计,
                [原料到发记录].[到货日期],
                [原料到发记录].[质检结果] 
from [原料到发记录] inner join [原料到发明细] on 
     [原料到发记录].ID=[原料到发明细].记录ID inner join 原料供货单位 on 
     原料供货单位.ID=[原料到发记录].供货ID  inner join 原料 on 原料.ID=[原料到发明细].[原料ID] " + Sql + " group by [原料到发记录].[ID],dbo.原料.品名,dbo.原料.钢级,[原料到发记录].[车号],原料供货单位.名称,[原料到发记录].[到货日期],[原料到发记录].[质检结果] order by ID desc";

            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
        }


    }
}
